/**
 * Created by wangshaohua on 2018/1/25.
 */
moduleContent.controller("AddContentTaskGroup", function (ExerciseContentResource, UploadFileService, $$CambioConfig, $scope, $log, $state, $stateParams, GeneralResource, LoadingService, ConstantService, DialogService) {
    initPage($scope);
    $scope.$on("ngDialog.opened", function () {
        dialogOnLoad();
        $scope.currentContentTask = {
            parentId: $scope.currentParentId,
            picture: LearningMapTemplateUtils.findExercise('TASK').picture
        };
    });

    // ------------------------------------------------------------------------------------------------------

    $scope.cancel = function () {
        DialogService.close();
    };

    $scope.submit = function () {
        $scope.currentContentTask.recordType = Enums.ExerciseContentRecordTypeEnum.CONTENT;
        $scope.currentContentTask.studyClassType = Enums.StudyClassTypeEnum.TASK;
        $scope.currentContentTask.space = Enums.ExerciseContentSpaceEnum.TASK;
        Alert.confirm(function () {
            ExerciseContentResource.saveQuiz({}, $scope.currentContentTask, function (result) {
                $scope.$parent.contentId = result.data.value;
                Alert.success();
                DialogService.close();
            });
        })
    }

    // ------------------------------------------------------------------------------------------------------
    // ------------------------------------------------------------------------------------------------------
    $scope.uploadPicture = function ($files) {
        if ($files && $files[0]) {
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'IMG', $scope).then(function (o) {
                $scope.currentContentTask.picture = o.id;
            });
        }
    };
});